package cn.com.hhrcw.web;

import cn.com.hhrcw.constant.CodeVConstant;
import cn.com.hhrcw.entity.CvWorkIntention;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.mapstruct.CvWorkIntentionConvert;
import cn.com.hhrcw.params.CommonParams;
import cn.com.hhrcw.params.CvWorkIntentionParams;
import cn.com.hhrcw.service.ICvWorkIntentionService;
import cn.com.hhrcw.utils.SubjectUtils;
import cn.com.hhrcw.vo.CvWorkIntentionVO;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 简历-求职意向(期望职位)
 *
 * @author jiangdong
 * @since 2020-10-29
 */
@RestController
@Slf4j
@RequestMapping("/personal/v1/cvWorkIntention")
@Api(tags = "简历-求职意向(期望职位)")
@Validated
public class CvWorkIntentionController extends JeecgController<CvWorkIntention, ICvWorkIntentionService> {

    @Autowired
    private ICvWorkIntentionService cvWorkntentionService;

    @Autowired
    private CvWorkIntentionConvert cvWorkIntentionConvert;


    /**
     * 我期望的职位分页列表查询（该接口暂不需要分页）
     *
     * @param params 请求参数
     * @return
     */
    @GetMapping(value = "/list")
    @AutoLog("简历-求职意向(期望职位)-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    public Result<List<CvWorkIntentionVO>> list(CvWorkIntentionParams params, HttpServletRequest request) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        QueryWrapper<CvWorkIntention> queryWrapper = QueryGenerator.initQueryWrapper(cvWorkIntentionConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        if (StringUtils.isBlank(params.getField())) {
            queryWrapper.orderByDesc("create_time");
        } else {
            queryWrapper.orderBy(true, params.getSort(), toUnderlineName(params.getField()));
        }
        queryWrapper.eq("user_id", sysUser.getId());
        List<CvWorkIntention> list = cvWorkntentionService.list(queryWrapper);
        List<CvWorkIntentionVO> voList = cvWorkIntentionConvert.toVO(list);
        List<CvWorkIntentionVO> collect = voList;
        return Result.ok(collect);
    }

    /**
     * 根据id查询
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @PermissionData
    public Result<CvWorkIntentionVO> queryById(String id) {
        CvWorkIntention cvWorkIntention = cvWorkntentionService
            .lambdaQuery()
            .eq(CvWorkIntention::getId, id)
            .eq(CvWorkIntention::getDelFlag, false)
            .one();
        if (cvWorkIntention == null) {
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(cvWorkIntentionConvert.toVO(cvWorkIntention));
    }

    /**
     * 求职意向添加
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog("简历-求职意向(期望职位)-添加")
    @ApiOperation(value = "添加", notes = "添加")
    public Result<?> add(@RequestBody @Valid CvWorkIntentionParams params) {

        SysUser sysUser = (SysUser) SubjectUtils.getSubject();

        CvWorkIntention cvWorkIntention = cvWorkIntentionConvert.toEntity(params);
        cvWorkIntention.setCreateBy(sysUser.getId());
        cvWorkIntention.setCreateTime(DateUtil.date());
        cvWorkIntention.setDelFlag(false);
        String[] workIndustryCode = params.getWorkIndustryCode();

        StringBuffer str1 = new StringBuffer();

        for (String s : workIndustryCode) {
            str1.append(s + ",");
        }
        cvWorkIntention.setWorkIndustry(str1.toString());
        String[] workSignCode = params.getCityStringCode();

        StringBuffer str2 = new StringBuffer();

        for (String s : workSignCode) {
            str2.append(s + ",");
        }
        cvWorkIntention.setCityId(str2.toString());
        String[] workTypeCode = params.getWorkNatureCode();

        StringBuffer str3 = new StringBuffer();

        for (String s : workTypeCode) {
            str3.append(s + ",");
        }
        cvWorkIntention.setWorkNature(str3.toString());
        boolean saved = cvWorkntentionService.save(cvWorkIntention);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 求职意向编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @AutoLog("简历-求职意向(期望职位)-编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    public Result<?> edit(@RequestBody @Valid CvWorkIntentionParams params) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();

        CvWorkIntention cvWorkIntention = cvWorkIntentionConvert.toEntity(params);
        cvWorkIntention.setUpdateBy(sysUser.getId());
        cvWorkIntention.setUpdateTime(DateUtil.date());
        String[] workIndustryCode = params.getWorkIndustryCode();

        StringBuffer str1 = new StringBuffer();

        for (String s : workIndustryCode) {
            str1.append(s + ",");
        }
        cvWorkIntention.setWorkIndustry(str1.toString());
        String[] workSignCode = params.getCityStringCode();

        StringBuffer str2 = new StringBuffer();

        for (String s : workSignCode) {
            str2.append(s + ",");
        }
        cvWorkIntention.setCityId(str2.toString());
        String[] workTypeCode = params.getWorkNatureCode();

        StringBuffer str3 = new StringBuffer();

        for (String s : workTypeCode) {
            str3.append(s + ",");
        }
        cvWorkIntention.setWorkNature(str3.toString());
        boolean updated = cvWorkntentionService.updateById(cvWorkIntention);
        Boolean hasKey = redisTemplate.hasKey(CodeVConstant.FINSH_INTENTION_CODE + sysUser.getId());
        if (updated && hasKey) {
            redisTemplate.delete(CodeVConstant.FINSH_INTENTION_CODE + sysUser.getId());
        }
        return updated ? Result.ok() : Result.error("编辑失败");
    }

    /**
     * 批量删除
     *
     * @param id 数据id
     * @return
     */
    @PostMapping(value = "/delete")
    @AutoLog("简历-求职意向(期望职位)-批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result delete(@RequestBody CommonParams params) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        CvWorkIntention intention = cvWorkntentionService.lambdaQuery()
            .eq(CvWorkIntention::getId, params.getId())
            .eq(CvWorkIntention::getUserId, sysUser.getId())
            .one();
        Assert.notNull(intention, "删除失败");
        intention.setDelFlag(true);
        boolean deleted = cvWorkntentionService.updateById(intention);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

    /**
     * 属性值是否重复
     *
     * @return
     */
    @Override
    @GetMapping("/propertyRepeat")
    @ApiOperation(value = "属性值是否重复", notes = "属性值是否重复")
    public Result propertyIsRepeat(@Valid AttributesValueParams params) {
        return super.propertyIsRepeat(params);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param params
     */
    @SneakyThrows
    @GetMapping(value = "/exportXls")
    @ApiOperation(value = "导出excel", notes = "导出excel")
    public Result exportXls(HttpServletRequest request, CvWorkIntentionParams params) {
        CvWorkIntention cvWorkIntention = cvWorkIntentionConvert.toEntity(params);
        cvWorkIntention.setDelFlag(false);
        QueryWrapper<CvWorkIntention> queryWrapper = QueryGenerator.initQueryWrapper(cvWorkIntention, request.getParameterMap());
        List<CvWorkIntention> data = cvWorkntentionService.list(queryWrapper);
        ExportParams exportParams = new ExportParams("简历-求职意向(期望职位)", "简历-求职意向(期望职位)");
        String url = super.exportXls(exportParams, CvWorkIntentionVO.class, cvWorkIntentionConvert.toVO(data));
        return Result.ok((Object) url);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CvWorkIntention.class);
    }

}
